package com.neo.service.websocket_service;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.springframework.stereotype.Component;

/** 
 * @Title: MyWebSocket.java 
 * @author 杨大锤 E-mail: 
 * @class MyWebSocket
 * @date 创建时间：2017年4月17日 下午1:54:06 
 * 
*/
@ServerEndpoint("/websocket")  
@Component  
public class MyWebSocket {
	

    private static int onlineCount = 0;  
  
    private static CopyOnWriteArraySet<MyWebSocket> webSocketSet = new CopyOnWriteArraySet<>();  
    private static Map<String, Session> clients = new ConcurrentHashMap<>();
    private Session session;  
  
    @OnOpen  
    public void onOpen (Session session){  
        this.session = session;  
        String name = session.getRequestParameterMap().get("userId").get(0);
        
        System.out.println("++++++++++++++++++++++++++___________________++++"+name);
        webSocketSet.add(this);
        clients.put(name, session);
        addOnlineCount();  
        System.out.println("有新链接加入!当前在线人数为" + getOnlineCount());  
    }  
  
    @OnClose  
    public void onClose (){  
        webSocketSet.remove(this);  
        subOnlineCount();  
        System.out.println("有一链接关闭!当前在线人数为" + getOnlineCount());  
    }  
  
    @OnMessage  
    public void onMessage (String message, Session session) throws IOException {  
        System.out.println("来自客户端的消息:" + message);  
        // 群发消息  
       /* for ( MyWebSocket item : webSocketSet ){  
                item.sendMessage(message);  
        }  */
        if(!session.getRequestParameterMap().get("userId").get(0).equals("test")){
        	 clients.get("test").getBasicRemote().sendText(message);
        }else{
        	sendInfo(message);
        }
        
    }  
    
  /**
   * 点对点发送
   * @param message
   * @throws IOException
   */
    public void sendMessage (String message) throws IOException {  
        this.session.getBasicRemote().sendText(message);  
    }  
    /**
     * 群发自定义消息
     * */
    public static void sendInfo(String message) throws IOException {
        for (MyWebSocket item : webSocketSet) {
            try {
                item.sendMessage(message);
            } catch (IOException e) {
                continue;
            }
        }
    }
    public static synchronized  int getOnlineCount (){  
        return MyWebSocket.onlineCount;  
    }  
  
    public static synchronized void addOnlineCount (){  
        MyWebSocket.onlineCount++;  
    }  
  
    public static synchronized void subOnlineCount (){  
        MyWebSocket.onlineCount--;  
    }  

	
	public static void put(String key,Session session) {
		clients.put(key, session);
	}
	
	public static Session get(String key) {
		return clients.get(key);
	}
	
	public static void remove(String key) {
		clients.remove(key);
	}
	
	public static void containsKey(String key) {
		clients.containsKey(key);
	}
	
	public static Map<String, Session> getMap() {
		return clients;
	}

}
